﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LogLineView : MonoBehaviour
{
    public int MaxChar = 20;
    public Button Btn;
    public Text LineText;
    public Action<LogLineView> onClick;

    public int LevelForDebug;
    public string DataForDebug;

    public int Level { get; private set; }
    public string Data { get; private set; }
    public string DataForShow { get; private set; }

    void Start()
    {
        Btn.onClick.AddListener(() => { 
            onClick?.Invoke(this);
        });
    }

    public void UpdateLine(int level, string data)
    {
        LevelForDebug = level;
        DataForDebug = data;

        Level = level;
        Data = data;

        var cnt = Mathf.Min(MaxChar, data.Length);
        var content = data.Substring(0, cnt);
        if (cnt < data.Length)
            content += "...";

        content = content.Replace("\n", "");
        DataForShow = data;

        if (Level == 1)
        {
            content = $"<color=yellow>{content}</color>";
            DataForShow = $"<color=yellow>{DataForShow}</color>";
        }
        else if (Level >= 2)
        {
            content = $"<color=red>{content}</color>";
            DataForShow = $"<color=red>{DataForShow}</color>";
        }

        LineText.text = content;
    }
}
